import { _decorator, Component, instantiate, Label, Node, Prefab, RichText, ScrollView } from 'cc';
import { Soldier, SoldierProIndex } from '../entity/Soldier';
import { PropertyItem } from './PropertyItem';
import { Utils } from '../common/Utils';
import { UserInfo } from '../common/UserInfo';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
const { ccclass, property } = _decorator;

@ccclass('TrainProperty')
export class TrainProperty extends Component {
    @property(Node)
    mask:Node
    @property(Node)
    close:Node
    @property(ScrollView)
    scrollView:ScrollView
    @property(Label)
    title:Label
    @property(Label)
    desc:Label
    @property(Node)
    proLayout:Node
    @property(Node)
    addLayout:Node
    @property(Label)
    wood:Label
    @property(Label)
    mud:Label
    @property(Label)
    iron:Label
    @property(Label)
    food:Label
    @property(Label)
    time:Label
    @property(Prefab)
    propertyItem:Prefab
    @property(Prefab)
    addRichText: Prefab

    start() {
        this.close.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.mask.on(Node.EventType.TOUCH_END, this.onClose, this)
    }

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.TrainProperty)
    }

    setData(soldier:Soldier){
        this.title.string = soldier.name + '属性'
        // 介绍
        this.desc.string = soldier.desc
        // 攻击
        let pro = instantiate(this.propertyItem)
        this.proLayout.addChild(pro)
        pro.getComponent(PropertyItem).setData(soldier, SoldierProIndex.StepAttack)
        // 移动速度
        pro = instantiate(this.propertyItem)
        this.proLayout.addChild(pro)
        pro.getComponent(PropertyItem).setData(soldier, SoldierProIndex.Speed)
        // 对步兵防御
        pro = instantiate(this.propertyItem)
        this.proLayout.addChild(pro)
        pro.getComponent(PropertyItem).setData(soldier, SoldierProIndex.StepDefense)
        // 运载量
        pro = instantiate(this.propertyItem)
        this.proLayout.addChild(pro)
        pro.getComponent(PropertyItem).setData(soldier, SoldierProIndex.Transport)
        // 对骑兵防御
        pro = instantiate(this.propertyItem)
        this.proLayout.addChild(pro)
        pro.getComponent(PropertyItem).setData(soldier, SoldierProIndex.RideDefense)
        // 消耗
        pro = instantiate(this.propertyItem)
        this.proLayout.addChild(pro)
        pro.getComponent(PropertyItem).setData(soldier, SoldierProIndex.Consume)
        //铁匠铺
        
        let advanced = soldier.getAttackAdvanced()
        let richText = instantiate(this.addRichText)
        this.addLayout.addChild(richText)
        richText.getComponent(Label).string = `【研究院】${soldier.name}改良等级 ${soldier.attackLevel}，攻击力+${(advanced.addition * 100).toFixed(2)}%`
        // 军械库
        advanced = soldier.getDefenseAdvanced()
        richText = instantiate(this.addRichText)
        this.addLayout.addChild(richText)
        richText.getComponent(Label).string = `【研究院】${soldier.name}改良等级 ${soldier.attackLevel}，防御力+${(advanced.addition * 100).toFixed(2)}%`
    
        this.wood.string = soldier.wood + ''
        this.mud.string = soldier.mud + ''
        this.iron.string = soldier.iron + ''
        this.food.string = soldier.food + ''
      
        this.time.string = Utils.secondToStr(soldier.trainTime)
        
        this.schedule(() => {
            this.scrollView.scrollToTop()
        }, 0.2, 0)
        
    }
}


